package com.jcfk.eam.controller.bd;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.enums.AvailableStatus;
import com.jcfk.common.business.enums.DeleteFlag;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.business.utils.DataBindHelper;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;

import com.jcfk.eam.domain.dto.base.EamFaultHandleMeasureDTO;
import com.jcfk.eam.domain.dto.base.EamFaultReasonDTO;
import com.jcfk.eam.domain.excel.base.EamFaultHandleMeasureExcel;
import com.jcfk.eam.domain.query.base.EamFaultHandleMeasureQuery;
import com.jcfk.eam.domain.query.base.EamFaultReasonQuery;
import com.jcfk.eam.domain.uploadExcel.base.EamFaultHandleMeasureUploadExcel;
import com.jcfk.eam.service.base.EamFaultHandleMeasureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

import com.alibaba.excel.EasyExcel;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

import com.jcfk.common.core.utils.ExcelUtils;

/**
 * 故障处理措施
 *
 * @author libinghai
 * @email 35738025@gqq.com
 * @date 2024-10-19
 */
@Api(tags = {"故障处理措施管理"})
@RestController
@RequestMapping("eamfaulthandlemeasure")
public class EamFaultHandleMeasureController {
    @Autowired
    private EamFaultHandleMeasureService eamFaultHandleMeasureService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamFaultHandleMeasureQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询故障处理措施")
    @PostMapping("/queryByPage")
    public RetPage<EamFaultHandleMeasureDTO> queryByPage(@RequestBody ReqPageParam<EamFaultHandleMeasureQuery> requestParam) {
        DataPage<EamFaultHandleMeasureDTO> page = new DataPage<EamFaultHandleMeasureDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
            EamFaultHandleMeasureDTO.class);
        page = eamFaultHandleMeasureService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     *  故障处理措施
     * 根据id 查询详情
     */
    @ApiOperation("查询故障处理措施")
    @GetMapping("/get/{id}")
    public RetResult<EamFaultHandleMeasureDTO> get(@PathVariable("id") String id) {
            EamFaultHandleMeasureDTO item = eamFaultHandleMeasureService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理故障处理措施
     */
    @ApiOperation("保存故障处理措施")
    @PostMapping("/save")
    @RequestLogHeader(modular = "故障处理措施管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamFaultHandleMeasureDTO eamFaultHandleMeasure) {
        // 数据校验
        RetResult ret = eamFaultHandleMeasureService.isValid(eamFaultHandleMeasure);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(eamFaultHandleMeasure.getId())){
            // 新增
            eamFaultHandleMeasureService.insert(eamFaultHandleMeasure);
        } else{
            // 更新
            eamFaultHandleMeasureService.update(eamFaultHandleMeasure);
        }
        return RetResult.ok();

    }

    /**
     *  批量删除故障处理措施
     */
    @ApiOperation("批量删除故障处理措施")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "故障处理措施管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamFaultHandleMeasureDTO eamFaultHandleMeasure) {
        return RetResult.ok(eamFaultHandleMeasureService.logicDeleteList(eamFaultHandleMeasure));
    }


    @PostMapping("export")
    @ApiOperation("导出")
    public void export(@RequestBody EamFaultHandleMeasureQuery requestParam, HttpServletResponse response) throws Exception {
        List<EamFaultHandleMeasureDTO> list = eamFaultHandleMeasureService.getList(requestParam);
        ExcelUtils.exportExcelToTarget(response, null, list, EamFaultHandleMeasureExcel.class);
    }

    /**
     * 导入故障处理措施
     */
    @PostMapping("upload")
    public RetResult<?> upload(MultipartFile file) throws IOException {
        // 读取excel
        List<EamFaultHandleMeasureUploadExcel> items = EasyExcel.read(file.getInputStream(), EamFaultHandleMeasureUploadExcel.class,null).autoCloseStream(true).sheet(0).headRowNumber(1).autoTrim(true).doReadSync();
        eamFaultHandleMeasureService.insertBatch(items);
        return RetResult.ok();
    }

    /**
     * 下载模板
     */
    @PostMapping("exportTemp")
    @ApiOperation("下载模板")
    public void exportTemp(HttpServletResponse response) throws Exception {
        List<EamFaultHandleMeasureUploadExcel> list = new ArrayList<>();
        list.add(new EamFaultHandleMeasureUploadExcel());
        ExcelUtils.exportExcelToTarget(response, null, list, EamFaultHandleMeasureUploadExcel.class);
    }


    /**
     *  删除故障处理措施
     */
    @ApiOperation("删除故障处理措施")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "故障处理措施管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamFaultHandleMeasureService.deleteitem(id);
        return RetResult.ok();
    }





    /**
     * 获取可用列表
     * @param query
     * @return
     */
    @PostMapping("/getList")
    public RetResult<List<EamFaultHandleMeasureDTO>> getAvailableList(@RequestBody EamFaultHandleMeasureQuery query) {
        if (query.getEnabled() == null) {
            query.setEnabled(AvailableStatus.ENABLE.getValue());
        }
        if (query.getDeleted() == null)
            query.setDeleted(DeleteFlag.NOTDELETED.getValue());
        List<EamFaultHandleMeasureDTO> list = eamFaultHandleMeasureService.getList(query);
        return RetResult.ok(list);
    }

}
